//
//  SelectGroupViewController.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/4/8.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "SelectGroupViewController.h"
#import "SelectGroupTableViewCell.h"

@interface SelectGroupViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *groupTableView; // 小组tableview
@property (nonatomic, strong) NSMutableArray *groupArray; // 小组数组
@property (nonatomic, strong) NSMutableDictionary *selectDic; // 被选中的小组

@property (nonatomic, strong) NSString *selectGroupName; // 保存选中小组的名字

@end

@implementation SelectGroupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // navi的修改
    [self changeNavigation];
    // 添加数据源
    [self addData];
    // 布局
    [self addMySubview];
}

// navi的修改
- (void)changeNavigation
{
    [self.navigationController.navigationBar setTranslucent:NO];
    self.navigationItem.title = @"移至分组";
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImageView.image = [UIImage imageNamed:@"houtui"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftImageView];
    UITapGestureRecognizer *leftTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lastPage:)];
    [leftImageView addGestureRecognizer:leftTapGes];
   
    
}

#pragma mark - 返回上一页面
- (void)lastPage:(UITapGestureRecognizer *)tapGes
{
    self.selectGroupBlock(self.selectGroupName);
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - 添加数据源
- (void)addData
{
    // 模拟数据
    self.groupArray = [NSMutableArray arrayWithObjects:@"我的好友", @"小学同学", @"初中同学", @"高中同学", @"大学同学", nil];
    // 被选中的字典
    self.selectDic = [NSMutableDictionary dictionary];
    for (NSString *key in self.groupArray) {
        [self.selectDic setValue:@"NO" forKey:key];
    }
    // 使我的好友为默认选项
    [self.selectDic setValue:@"YES" forKey:@"我的好友"];
    NSLog(@"self.selectDic = %@", self.selectDic);
    
    // 初始化选中小组的名字
    self.selectGroupName = nil;
}

#pragma mark - 布局
- (void)addMySubview
{
    self.groupTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, (kScreenHeight - 64) * kAdjustParamater) style:UITableViewStylePlain];
    self.groupTableView.backgroundColor = [UIColor whiteColor];
    self.groupTableView.dataSource = self;
    self.groupTableView.delegate = self;
    [self.view addSubview:_groupTableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.groupArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"selectGroup";
    SelectGroupTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[SelectGroupTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSString *key = self.groupArray[indexPath.row];
    cell.groupNameLabel.text = key;
    // 获取是否点击
    cell.isSelect = [self.selectDic valueForKey:key];
    NSLog(@"%@",[self.selectDic valueForKey:key]);
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSString *key = self.groupArray[indexPath.row];
    // 先将字典里面全部改为未选中状态
    for (int i = 0; i < self.groupArray.count; i++) {
        NSString *tempKey = self.groupArray[i];
        [self.selectDic setValue:@"NO" forKey:tempKey];
    }
    // 选中当前
    [self.selectDic setValue:@"YES" forKey:key];
    self.selectGroupName = key;
    [self.groupTableView reloadData];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
